package com.fastjpa.service.impl;

import java.util.List;
import org.springframework.stereotype.Service;
import com.fastjpa.dao.TeacherDao;
import com.fastjpa.entity.Teacher;
import com.fastjpa.vo.TeacherVo;
import com.fastjpa.service.TeacherService;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;
import com.efeichong.common.PageData;
import com.efeichong.util.TransformUtils;
import com.efeichong.jpa.JExample;
import com.efeichong.exception.JpaException;

/**
* @author admin
* @date 2022/07/17
* @description 教师
*/
@RequiredArgsConstructor
@Service
public class TeacherServiceImpl implements TeacherService {

    private final TeacherDao teacherDao;

    /**
     * 通过id查询
     *
     * @param id 主键
     * @return
     */
    @Override
    public TeacherVo getInfo(Long id){
        return teacherDao.selectById(id, TeacherVo.class).orElseThrow(() -> new JpaException("数据不存在！"));
    }

    /**
    * 分页查询
    *
    * @param teacherVo
    * @return
    */
    @Override
    public PageData<TeacherVo> selectByPage(TeacherVo teacherVo){
        JExample<Teacher> example = new JExample();
        example.initExample(teacherVo);
        return teacherDao.selectByPage(example, TeacherVo.class);
    }

    /**
     * 新增
     *
     * @param teacherVo
     * @return
     */
    @Transactional
    @Override
    public void insertTeacher(TeacherVo teacherVo){
        teacherDao.save(TransformUtils.toPo(teacherVo,Teacher.class));
    }

    /**
     * 修改
     *
     * @param teacherVo
     * @return
     */
    @Transactional
    @Override
    public void updateTeacher(TeacherVo teacherVo){
        if(teacherVo.getId() == null){
            throw new JpaException("主键为必传参数");
        }
        teacherDao.save(TransformUtils.toPo(teacherVo,Teacher.class));
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids){
        teacherDao.deleteAllByIds(ids);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public void deleteById(Long id){
        teacherDao.deleteById(id);
    }
}
